package com.campus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campus.constant.PasswordConstant;
import com.campus.constant.RoleConstant;
import com.campus.dao.ManagerTeacherMapper;
import com.campus.entry.ManagerTeacher;
import com.campus.pojo.dto.RegisterDto;
import com.campus.pojo.dto.TeacherDto;
import com.campus.service.AccountService;
import com.campus.service.ManagerTeacherService;
import com.campus.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ManagerTeacherServiceImpl implements ManagerTeacherService {

    @Autowired
    private ManagerTeacherMapper managerTeacherMapper;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private AccountService accountService;

    /**
     * 插入教师
     * @param managerTeacher
     */
    @Override
    public void saveTeacher(ManagerTeacher managerTeacher) {
        String loginName=managerTeacher.getTeacherName();
        RegisterDto registerDto=new RegisterDto(loginName, PasswordConstant.DEFAULT_PASSWORD, RoleConstant.TEACHER);
        accountService.register(registerDto);
        Integer userIdByUserName = sysUserService.getUserIdByUserName(loginName);
        managerTeacher.setLoginId(userIdByUserName);
        managerTeacherMapper.insert(managerTeacher);
    }

    /**
     * 根据Id查询教师
     * @param managerTeacherId
     * @return {@link ManagerTeacher }
     */
    @Override
    public ManagerTeacher getTeacherById(Integer managerTeacherId) {
        LambdaQueryWrapper<ManagerTeacher> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ManagerTeacher::getId, managerTeacherId);
        return managerTeacherMapper.selectOne(lambdaQueryWrapper);
    }

    /**
     * 根据教师信息筛选教师
     * @param teacherDto
     * @return {@link List }<{@link ManagerTeacher }>
     */
    @Override
    public List<ManagerTeacher> getTeacherBy(TeacherDto teacherDto) {
        LambdaQueryWrapper<ManagerTeacher> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (isNotNull(teacherDto.getTeacherName())) {
            lambdaQueryWrapper.eq(ManagerTeacher::getTeacherName, teacherDto.getTeacherName());
        }
        if (isNotNull(teacherDto.getProfession())) {
            lambdaQueryWrapper.eq(ManagerTeacher::getProfession, teacherDto.getProfession());
        }
        if (isNotNull(teacherDto.getSchool())) {
            lambdaQueryWrapper.eq(ManagerTeacher::getSchool, teacherDto.getSchool());
        }
        if (isNotNull(teacherDto.getSubject())) {
            lambdaQueryWrapper.eq(ManagerTeacher::getSubject, teacherDto.getSubject());
        }
        return managerTeacherMapper.selectList(lambdaQueryWrapper);
    }

    /**
     * 更新教师信息
     * @param managerTeacher
     */
    @Override
    public void updateTeacher(ManagerTeacher managerTeacher) {
        if (managerTeacher.getId() == null) {
            throw new IllegalArgumentException("教师ID不能为空");
        }
        managerTeacherMapper.updateById(managerTeacher);
    }

    /**
     * 删除教师
     * @param id
     */
    @Override
    public void deleteTeacher(Integer id) {
        if (id == null) {
            throw new IllegalArgumentException("教师ID不能为空");
        }
        managerTeacherMapper.deleteById(id);
    }

    @Override
    public Page<ManagerTeacher> listTeachers(Integer pageNum, Integer pageSize) {
        return null;
    }

    /**
     * 分页查询教师
     * @param pageNum
     * @param pageSize
     * @return {@link Page}<{@link ManagerTeacher }>
     */
    @Override
    public Page<ManagerTeacher> listTeachers(Integer pageNum, Integer pageSize, TeacherDto teacherDto) {
        // 创建分页对象
        Page<ManagerTeacher> page = new Page<>(pageNum, pageSize);

        // 创建 LambdaQueryWrapper 用于动态构建查询条件
        LambdaQueryWrapper<ManagerTeacher> queryWrapper = new LambdaQueryWrapper<>();

        // 根据传入的筛选条件添加查询条件
        if (teacherDto.getTeacherName() != null && !teacherDto.getTeacherName().isEmpty()) {
            queryWrapper.like(ManagerTeacher::getTeacherName, teacherDto.getTeacherName());
        }
        if (teacherDto.getSubject() != null && !teacherDto.getSubject().isEmpty()) {
            queryWrapper.like(ManagerTeacher::getSubject, teacherDto.getSubject());
        }
        if (teacherDto.getSchool() != null && !teacherDto.getSchool().isEmpty()) {
            queryWrapper.like(ManagerTeacher::getSchool, teacherDto.getSchool());
        }
        if (teacherDto.getProfession() != null && !teacherDto.getProfession().isEmpty()) {
            queryWrapper.like(ManagerTeacher::getProfession, teacherDto.getProfession());
        }

        // 执行分页查询
        return managerTeacherMapper.selectPage(page, queryWrapper);
    }





    private boolean isNotNull(String s) {
        return s != null && !s.isEmpty();
    }

    /**
     * 获取教师总数
     * @return {@link Long }
     */
    @Override
    public Long conutTeacher() {
        return managerTeacherMapper.selectCount(null);
    }

    @Override
    public Integer getTeacherIdByLoginId(Integer loginId) {
       return managerTeacherMapper.getTeacherIdByLoginId(loginId);

    }
}
